package com.ruoyi.util;

import com.ruoyi.mapper.AdsRecoverStationMapper;
import com.ruoyi.mapper.VideoRecoverStationMapper;
import com.ruoyi.mapper.RecoverStationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class TimeTask {

    @Autowired
    private AdsRecoverStationMapper adsRecoverStationMapper;

    @Autowired
    private VideoRecoverStationMapper videoRecoverStationMapper;

    @Autowired
    private RecoverStationMapper recoverStationMapper;

    // 每天凌晨1点执行
    @Scheduled(cron = "0 0 0 * * ?")
    public void updateValidDateAndClean() {
        // 所有记录的valid_date字段减1
        adsRecoverStationMapper.decreaseValidDate();

        // 删除valid_date为-1的记录
        adsRecoverStationMapper.deleteByValidDate(-1);
    }

    @Scheduled(cron = "0 0 0 * * ?")
    public void deleteVideoRecoverDataBefore() {
        videoRecoverStationMapper.decreaseValidDate();
        videoRecoverStationMapper.deleteByValidDate(-1);

    }

    // 每天凌晨2点执行文章回收站清理任务
    @Scheduled(cron = "0 0 0 * * ?")
    public void cleanRecoverStationData() {
        // 递减文章回收站记录的valid_date字段
        recoverStationMapper.decreaseValidDate();
        
        // 删除valid_date为-1的记录
        recoverStationMapper.deleteByValidDate(-1);
    }
}
